﻿namespace MicroCloud.Api.Systems.Areas.Admin.Controllers
{
    /// <summary>
    /// 字典信息
    /// </summary>
    [Description("字典信息")]
    [ModuleInfo(Code = "Dict", Name = "字典信息", OrderNo = 1, Position = "Systems", PositionName = "系统管理模块")]
    public class DictController : AdminApiControllerBase
    {
        protected IDictService DictService => ServiceProvider.GetService<IDictService>();

        #region Dict
        /// <summary>
        /// 读取字典项列表
        /// </summary>
        /// <param name="request">分布查询请求</param>
        /// <returns>字典项列表</returns>
        [HttpPost]
        [Description("读取字典")]
        [ModuleInfo]
        public PageData<DictOutputDto> ReadDict(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Dict, bool>> predicate = FilterService.GetExpression<Dict>(request.FilterGroup);
            var page = CacheService.ToPageCache<Dict, DictOutputDto>(DictService.Dicts, predicate, request.PageCondition, function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        [Description("新增字典")]
        [UnitOfWork]
        [HttpPost]
        [ModuleInfo]
        public async Task<AjaxResult> CreateDict(DictInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<DictOutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await DictService.CreateDicts(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<DictOutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("制氧机版本号实体“{0}”创建成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 更新字典
        /// </summary>
        [HttpPost]
        [UnitOfWork]
        [ModuleInfo]
        [Description("更新字典")]
        public async Task<AjaxResult> UpdateDict(DictInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<DictOutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await DictService.UpdateDicts(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<DictOutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("制氧机版本实体“{0}”创建成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        [HttpPost]
        [UnitOfWork]
        [ModuleInfo]
        [Description("删除字典")]
        public async Task<AjaxResult> DeleteDict(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await DictService.DeleteDicts(ids);
            return result.ToAjaxResult();
        }

        #endregion

        #region DictEntry
        /// <summary>
        /// 读取字典项列表
        /// </summary>
        /// <param name="request">分布查询请求</param>
        /// <returns>字典项列表</returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取字典项")]
        public PageData<DictEntryOutputDto> ReadDictEntry(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<DictEntry, bool>> predicate = FilterService.GetExpression<DictEntry>(request.FilterGroup);
            var page = CacheService.ToPageCache<DictEntry, DictEntryOutputDto>(DictService.DictEntrys, predicate, request.PageCondition, function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增字典项
        /// </summary>
        [Description("新增字典项")]
        [UnitOfWork]
        [HttpPost]
        [ModuleInfo]
        public async Task<AjaxResult> CreateDictEntry(DictEntryInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<DictEntryOutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await DictService.CreateDictEntrys(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<DictEntryOutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("制氧机版本号实体“{0}”创建成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 更新字典项
        /// </summary>
        [HttpPost]
        [UnitOfWork]
        [ModuleInfo]
        [Description("更新字典项")]
        public async Task<AjaxResult> UpdateDictEntry(DictEntryInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<DictEntryOutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await DictService.UpdateDictEntrys(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<DictEntryOutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("制氧机版本实体“{0}”创建成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 删除字典项
        /// </summary>
        [HttpPost]
        [ModuleInfo]
        [UnitOfWork]
        [Description("删除字典项")]
        public async Task<AjaxResult> DeleteDictEntry(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await DictService.DeleteDictEntrys(ids);
            return result.ToAjaxResult();
        }

        #endregion
    }
}